package com.kermel.andromeda.engine;

import java.util.Timer;
import java.util.TimerTask;

import com.kermel.andromeda.data.TimeRemaining;

public class CountdownTicker {
    public static final String TIMER_NAME = "CountdownTickerTimer";
    
    private CountdownSubscriber subscriber;
    private Timer timer;
    private TimerTask timerTask;
    private TimeRemaining timeRemaining;
    
    public CountdownTicker(CountdownSubscriber subscriber) {
        this.subscriber = subscriber;
        
        timerTask = new TimerCallback();
        timer = new Timer(TIMER_NAME);
    }
    
    public void start(int hours, int minutes) {
        timeRemaining = new TimeRemaining(hours, minutes);
        timer.schedule(timerTask, 0, 1000);
    }
    
    private class TimerCallback extends TimerTask {
        @Override
        public void run() {
            if (timeRemaining.getHours() == 0 && timeRemaining.getMinutes() == 0 && timeRemaining.getSeconds() == 0)
                System.out.println();
            
            timeRemaining.decrementOneSecond();
            
            
            if (subscriber != null)
                subscriber.onCountdownTick(timeRemaining);
        }
    }
}
